#include "header/pyramid_room1_objects.h"
#include "header/pyramid_rotaryBody.h"
#include "header/pyramid_coffin.h"
#include "header/pyramid_torch.h"

OSG_USING_NAMESPACE

NodePtr createRoom1Objects()
{
	Matrix m;

	//Saeulen Texturen laden
	ImagePtr cryptCylImage = Image::create();
	cryptCylImage->read("materials/sand.jpg");
		
		
	/******************************
	 * rotaryBodys Anfang
	 * ****************************/
	
	//rotaryBody1
	NodePtr rotaryBody1 = createRotaryBody();

	TransformPtr tRotaryBody1 = Transform::create();
	beginEditCP(tRotaryBody1, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(50, 10, 60);
		//m.setTranslate(120, 0, 1200);
		m.setScale(2);
		tRotaryBody1->setMatrix(m);
	endEditCP(tRotaryBody1, Transform::MatrixFieldMask);
	
	NodePtr rotaryBody1Trans = Node::create();
	beginEditCP(rotaryBody1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		rotaryBody1Trans->setCore(tRotaryBody1);
		rotaryBody1Trans->addChild(rotaryBody1);
	endEditCP(rotaryBody1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	//rotaryBody2
	NodePtr rotaryBody2 = createRotaryBody();

	TransformPtr tRotaryBody2 = Transform::create();
	beginEditCP(tRotaryBody2, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(200, 10, 60);
		m.setScale(2);
		tRotaryBody2->setMatrix(m);
	endEditCP(tRotaryBody2, Transform::MatrixFieldMask);
	
	NodePtr rotaryBody2Trans = Node::create();
	beginEditCP(rotaryBody2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		rotaryBody2Trans->setCore(tRotaryBody2);
		rotaryBody2Trans->addChild(rotaryBody2);
	endEditCP(rotaryBody2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	//rotaryBody3
	NodePtr rotaryBody3 = createRotaryBody();

	TransformPtr tRotaryBody3 = Transform::create();
	beginEditCP(tRotaryBody3, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(20, 0, 380);
		m.setScale(3);
		tRotaryBody3->setMatrix(m);
	endEditCP(tRotaryBody3, Transform::MatrixFieldMask);
	
	NodePtr rotaryBody3Trans = Node::create();
	beginEditCP(rotaryBody3Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		rotaryBody3Trans->setCore(tRotaryBody3);
		rotaryBody3Trans->addChild(rotaryBody3);
	endEditCP(rotaryBody3Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	//rotaryBody4
	NodePtr rotaryBody4 = createRotaryBody();

	TransformPtr trotaryBody4 = Transform::create();
	beginEditCP(trotaryBody4, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(230, 0, 380);
		m.setScale(3);
		trotaryBody4->setMatrix(m);
	endEditCP(trotaryBody4, Transform::MatrixFieldMask);
	
	NodePtr rotaryBody4Trans = Node::create();
	beginEditCP(rotaryBody4Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		rotaryBody4Trans->setCore(trotaryBody4);
		rotaryBody4Trans->addChild(rotaryBody4);
	endEditCP(rotaryBody4Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	

	
	
	
	
	
	
	//Torch1
	NodePtr cryptTorch1 = createTorch();
	//Transformation
	TransformPtr tcryptTorch1 = Transform::create();
	beginEditCP(tcryptTorch1, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(4.5, 42, 160);
		tcryptTorch1->setMatrix(m);
	endEditCP(tcryptTorch1, Transform::MatrixFieldMask);
	
	NodePtr cryptTorch1Trans = Node::create();
	beginEditCP(cryptTorch1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptTorch1Trans->setCore(tcryptTorch1);
		cryptTorch1Trans->addChild(cryptTorch1);
	endEditCP(cryptTorch1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	//Torch2
	NodePtr cryptTorch2 = createTorch();
	//Transformation
	TransformPtr tcryptTorch2 = Transform::create();
	beginEditCP(tcryptTorch2, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(245.5, 42, 160);
		m.setRotate(Quaternion(Vec3f(0,1,0), +3.14159));
		tcryptTorch2->setMatrix(m);
	endEditCP(tcryptTorch2, Transform::MatrixFieldMask);
	
	NodePtr cryptTorch2Trans = Node::create();
	beginEditCP(cryptTorch2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptTorch2Trans->setCore(tcryptTorch2);
		cryptTorch2Trans->addChild(cryptTorch2);
	endEditCP(cryptTorch2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	//Torch3
	NodePtr cryptTorch3 = createTorch();
	//Transformation
	TransformPtr tcryptTorch3 = Transform::create();
	beginEditCP(tcryptTorch3, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(170, 42, 395.5);
		m.setRotate(Quaternion(Vec3f(0,1,0), +3.14159/2));
		tcryptTorch3->setMatrix(m);
	endEditCP(tcryptTorch3, Transform::MatrixFieldMask);
	
	NodePtr cryptTorch3Trans = Node::create();
	beginEditCP(cryptTorch3Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptTorch3Trans->setCore(tcryptTorch3);
		cryptTorch3Trans->addChild(cryptTorch3);
	endEditCP(cryptTorch3Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	//Torch4
	NodePtr cryptTorch4 = createTorch();
	//Transformation
	TransformPtr tcryptTorch4 = Transform::create();
	beginEditCP(tcryptTorch4, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(80, 42, 395.5);
		m.setRotate(Quaternion(Vec3f(0,1,0), +3.14159/2));
		tcryptTorch4->setMatrix(m);
	endEditCP(tcryptTorch4, Transform::MatrixFieldMask);
	
	NodePtr cryptTorch4Trans = Node::create();
	beginEditCP(cryptTorch4Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptTorch4Trans->setCore(tcryptTorch4);
		cryptTorch4Trans->addChild(cryptTorch4);
	endEditCP(cryptTorch4Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	/***************************
	 * Coffin start
	 * *************************/
	
	NodePtr coffin = createCoffin();
	TransformPtr tCoffin = Transform::create();
	beginEditCP(tCoffin, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(75, 0, 75);

		tCoffin->setMatrix(m);
	 endEditCP(tCoffin, Transform::MatrixFieldMask);
	
	 NodePtr coffinTrans = Node::create();
	 beginEditCP(coffinTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		coffinTrans->setCore(tCoffin);
		coffinTrans->addChild(coffin);
	endEditCP(coffinTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
		
	/***************************************
	 * Säulen anfang
	 * ************************************/
	
	/* Säulen Parameter
		+    \param[in] height Height of the cylinder.
		+    \param[in] radius Radius of the cylinder.
		+    \param[in] sides Number of sides the base is subdivided into.
		+    \param[in] doSide If true, side faces are created.
		+    \param[in] doTop If true, top cap faces are created.
		+    \param[in] doBttom If true, bottom cap faces are created.
	*/
	Real32 cyl_height = 120;
	Real32 cyl_radius = 12;
	UInt16 cyl_sides = 80;
	bool cyl_doSide = true;
	bool cyl_doTop = true;
	bool cyl_doBttom = true;
	
	
	// *********** Saeule 1 rechts***********
	NodePtr cryptCyl1 = makeCylinder( cyl_height, cyl_radius, cyl_sides, cyl_doSide, cyl_doTop, cyl_doBttom );
	TransformPtr tcryptCyl1 = Transform::create();
	beginEditCP(tcryptCyl1, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(200, 60, 100);
		tcryptCyl1->setMatrix(m);
	endEditCP(tcryptCyl1, Transform::MatrixFieldMask);
	
	NodePtr cryptCyl1Trans = Node::create();
	beginEditCP(cryptCyl1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptCyl1Trans->setCore(tcryptCyl1);
		cryptCyl1Trans->addChild(cryptCyl1);
	endEditCP(cryptCyl1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	// *********** Saeule 2 links***********
	NodePtr cryptCyl2 = makeCylinder( cyl_height, cyl_radius, cyl_sides, cyl_doSide, cyl_doTop, cyl_doBttom );
	TransformPtr tcryptCyl2 = Transform::create();
	beginEditCP(tcryptCyl2, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(50, 60, 100);
		tcryptCyl2->setMatrix(m);
	endEditCP(tcryptCyl2, Transform::MatrixFieldMask);
	
	NodePtr cryptCyl2Trans = Node::create();
	beginEditCP(cryptCyl2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptCyl2Trans->setCore(tcryptCyl2);
		cryptCyl2Trans->addChild(cryptCyl2);
	endEditCP(cryptCyl2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	// *********** Saeule 3 rechts***********
	NodePtr cryptCyl3 = makeCylinder( cyl_height, cyl_radius, cyl_sides, cyl_doSide, cyl_doTop, cyl_doBttom );
	TransformPtr tcryptCyl3 = Transform::create();
	beginEditCP(tcryptCyl3, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(200, 60, 220);
		tcryptCyl3->setMatrix(m);
	endEditCP(tcryptCyl3, Transform::MatrixFieldMask);
	
	NodePtr cryptCyl3Trans = Node::create();
	beginEditCP(cryptCyl3Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptCyl3Trans->setCore(tcryptCyl3);
		cryptCyl3Trans->addChild(cryptCyl3);
	endEditCP(cryptCyl3Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	// *********** Saeule 4 links***********
	NodePtr cryptCyl4 = makeCylinder( cyl_height, cyl_radius, cyl_sides, cyl_doSide, cyl_doTop, cyl_doBttom );
	TransformPtr tcryptCyl4 = Transform::create();
	beginEditCP(tcryptCyl4, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(50, 60, 220);
		tcryptCyl4->setMatrix(m);
	endEditCP(tcryptCyl4, Transform::MatrixFieldMask);
	
	NodePtr cryptCyl4Trans = Node::create();
	beginEditCP(cryptCyl4Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptCyl4Trans->setCore(tcryptCyl4);
		cryptCyl4Trans->addChild(cryptCyl4);
	endEditCP(cryptCyl4Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	// *********** Saeule 5 rechts***********
	NodePtr cryptCyl5 = makeCylinder( cyl_height, cyl_radius, cyl_sides, cyl_doSide, cyl_doTop, cyl_doBttom );
	TransformPtr tcryptCyl5 = Transform::create();
	beginEditCP(tcryptCyl5, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(200, 60, 340);
		tcryptCyl5->setMatrix(m);
	endEditCP(tcryptCyl5, Transform::MatrixFieldMask);
	
	NodePtr cryptCyl5Trans = Node::create();
	beginEditCP(cryptCyl5Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptCyl5Trans->setCore(tcryptCyl5);
		cryptCyl5Trans->addChild(cryptCyl5);
	endEditCP(cryptCyl5Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	// *********** Saeule 6 links***********
	NodePtr cryptCyl6 = makeCylinder( cyl_height, cyl_radius, cyl_sides, cyl_doSide, cyl_doTop, cyl_doBttom );
	TransformPtr tcryptCyl6 = Transform::create();
	beginEditCP(tcryptCyl6, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(50, 60, 340);
		tcryptCyl6->setMatrix(m);
	endEditCP(tcryptCyl6, Transform::MatrixFieldMask);
	
	NodePtr cryptCyl6Trans = Node::create();
	beginEditCP(cryptCyl6Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		cryptCyl6Trans->setCore(tcryptCyl6);
		cryptCyl6Trans->addChild(cryptCyl6);
	endEditCP(cryptCyl6Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	/***************************************
	 * Säulen Ende
	 * ************************************/
	
	/******************************
	 * Texturen definition start
	 * ****************************/

	
		
	TextureChunkPtr cryptCylTex = TextureChunk::create();
	beginEditCP(cryptCylTex);
		cryptCylTex->setImage(cryptCylImage);
		cryptCylTex->setMinFilter(GL_LINEAR);
		cryptCylTex->setMagFilter(GL_LINEAR);
		cryptCylTex->setWrapS(GL_REPEAT);
		cryptCylTex->setWrapT(GL_REPEAT);
		cryptCylTex->setEnvMode(GL_MODULATE);
	endEditCP(cryptCylTex);
	
	SimpleMaterialPtr cryptCylMat = SimpleMaterial::create();
	beginEditCP(cryptCylMat);
		cryptCylMat->setAmbient(Color3f(0.3, 0.3, 0.3)); 
		cryptCylMat->setDiffuse(Color3f(1.0, 1.0, 1.0));
		cryptCylMat->addChunk(cryptCylTex);
	endEditCP(cryptCylMat);
		
	GeometryPtr cryptCylGeo = GeometryPtr::dcast(cryptCyl1->getCore());
	beginEditCP(cryptCylGeo);
		cryptCylGeo->setMaterial(cryptCylMat);
	endEditCP(cryptCylGeo);
	
	cryptCylGeo = GeometryPtr::dcast(cryptCyl2->getCore());
	beginEditCP(cryptCylGeo);
		cryptCylGeo->setMaterial(cryptCylMat);
	endEditCP(cryptCylGeo);
	
	cryptCylGeo = GeometryPtr::dcast(cryptCyl3->getCore());
	beginEditCP(cryptCylGeo);
		cryptCylGeo->setMaterial(cryptCylMat);
	endEditCP(cryptCylGeo);
	
	cryptCylGeo = GeometryPtr::dcast(cryptCyl4->getCore());
	beginEditCP(cryptCylGeo);
		cryptCylGeo->setMaterial(cryptCylMat);
	endEditCP(cryptCylGeo);
	
	cryptCylGeo = GeometryPtr::dcast(cryptCyl5->getCore());
	beginEditCP(cryptCylGeo);
		cryptCylGeo->setMaterial(cryptCylMat);
	endEditCP(cryptCylGeo);
	
	cryptCylGeo = GeometryPtr::dcast(cryptCyl6->getCore());
	beginEditCP(cryptCylGeo);
		cryptCylGeo->setMaterial(cryptCylMat);
	endEditCP(cryptCylGeo);
	
	/******************************
	 * Texturen definition Ende
	 * ****************************/
	
	
	
	
	
	
	// Erstellen eines Knotens
	NodePtr room1Objects = Node::create();
	beginEditCP(room1Objects, Node::CoreFieldMask);
	{
		room1Objects->setCore(Group::create());
		room1Objects->addChild(cryptCyl1Trans);
		room1Objects->addChild(cryptCyl2Trans);
		room1Objects->addChild(cryptCyl3Trans);
		room1Objects->addChild(cryptCyl4Trans);
		room1Objects->addChild(cryptCyl5Trans);
		room1Objects->addChild(cryptCyl6Trans);
		room1Objects->addChild(cryptTorch1Trans);
		room1Objects->addChild(cryptTorch2Trans);
		//room1Objects->addChild(cryptTorch3Trans);
		//room1Objects->addChild(cryptTorch4Trans);
		room1Objects->addChild(coffinTrans);
		room1Objects->addChild(rotaryBody1Trans);
		room1Objects->addChild(rotaryBody2Trans);		
		room1Objects->addChild(rotaryBody3Trans);
		room1Objects->addChild(rotaryBody4Trans);
	}
	endEditCP  (room1Objects, Node::CoreFieldMask);
	
	
	return room1Objects;
}
